import Toolkit from '../core/toolkit';

class Generator {

    matrix;
    orders;

    generate() {
        while (!this.internalGenerate()) {

        }
    }

    internalGenerate() {
        this.matrix = Toolkit.matrix.makeMatrix();
        this.orders = Toolkit.matrix.makeMatrix()
            .map(row => row.map((v, i) => i))
            .map(row => Toolkit.matrix.shuffle(row));

        for (let i = 1; i <= 9; i++) {
            if (!this.fillNumber(i)) {
                return false
            }
        }

        return true;
    }

    fillNumber(n) {
        return this.fillRow(n, 0);
    }

    fillRow(n, rowIndex) {
        if (rowIndex > 8) {
            return true;
        }

        const row = this.matrix[rowIndex];
        const orders = this.orders[rowIndex];
        for (let i = 0; i < 9; i++) {
            const colIndex = orders[i];
            if (row[colIndex]) {
                continue;
            }

            if (!Toolkit.matrix.checkFillAble(this.matrix, n, rowIndex, colIndex)) {
                continue;
            }

            row[colIndex] = n;
            if (!this.fillRow(n, rowIndex + 1)) {
                row[colIndex] = 0;
                continue;
            }

            return true;
        }

        return false;
    }
}

export default Generator;
